package com.wwwarehouse.taskcenter.adapter;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.bean.BluetoothBean;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.eventbus_event.ToConnectEvent;

import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * Description: 工具分类
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/9/15 13:52
 */

public class BluetoothListAdapter extends RecyclerView.Adapter<BluetoothListAdapter.ViewHolder> {

    private Context mContext = null;
    private List<BluetoothBean> mScanData;

    public BluetoothListAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<BluetoothBean> mScanData) {
        this.mScanData = mScanData;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ble_list_adapter, parent, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.mRelItem.setTag(position);
        holder.mRelItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogTools.getInstance().showCustomDialogPrompt(mContext, "提示", "" + mContext.getString(R.string.new_task_connectthis_ring), false, new DialogTools.ConfirmListener() {
                    @Override
                    public void setConfirmListener(Dialog dialog, View view, String s) {
                        //确定
                        dialog.dismiss();
                        EventBus.getDefault().post(new ToConnectEvent(mScanData.get(position), position));

                        for (int i = 0; i < mScanData.size(); i++) {
                            mScanData.get(i).setState("DisConnected");
                            if (position == (int) holder.mRelItem.getTag()) {
                                mScanData.get(position).setState("Connecting");
                            }
                        }
                        notifyDataSetChanged();
                    }
                }, "连接", "取消");
            }
        });
        if ("Connecting".equals(mScanData.get(position).getState())) {
            holder.mState.setVisibility(View.VISIBLE);
            holder.mTextView.setSelected(false);
            LogUtils.showLog("BluetoothListAdapter----->Connecting");
            holder.mState.setImageResource(R.drawable.loading);
        } else if ("DisConnected".equals(mScanData.get(position).getState())) {
            holder.mState.setVisibility(View.GONE);
            LogUtils.showLog("BluetoothListAdapter----->DisConnected");
            holder.mTextView.setSelected(false);
        } else if ("ConnectSuccess".equals(mScanData.get(position).getState())) {
            holder.mState.setVisibility(View.VISIBLE);
            holder.mState.setImageResource(R.drawable.connected);
            LogUtils.showLog("BluetoothListAdapter----->ConnectSuccess");
            holder.mTextView.setSelected(true);
        }
        if (!TextUtils.isEmpty(mScanData.get(position).getName())) {
            holder.mTextView.setText(mScanData.get(position).getName());
        } else {
            holder.mTextView.setText(mScanData.get(position).getAddress());
        }
    }

    @Override
    public int getItemCount() {
        return mScanData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ImageView mState;
        public RelativeLayout mRelItem;

        public ViewHolder(View view) {
            super(view);
            mTextView = (TextView) view.findViewById(R.id.tv_name);
            mState = (ImageView) view.findViewById(R.id.iv_state);
            mRelItem = (RelativeLayout) view.findViewById(R.id.rel_item);
        }
    }
}
